﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class EquipmentProfile : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkSysCreated;
        protected HtmlForm F2631;
        protected HtmlInputHidden hdnEquipmentProfileID;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblDescr;
        protected Label lblEquipmentProfile;
        protected Label lblEquipmentProFileID;
        protected Label lblMaximumWeight;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescr;
        protected TextBox txtEquipmentProFileID;
        protected TextBox txtMaximumWeight;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtEquipmentProFileID.Text = "";
            this.txtEquipmentProFileID.Enabled = true;
            this.hdnEquipmentProfileID.Value = "";
            this.chkSysCreated.Checked = false;
            this.lblMsg.Text = "";
            this.lblMsg.Visible = false;
            this.hdnFlag.Value = "0";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.chkSysCreated.Checked)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C011");
            }
            else if (!string.IsNullOrEmpty(this.txtEquipmentProFileID.Text.Trim()))
            {
                try
                {
                    List<string> areaID = new List<string> {
                        this.txtEquipmentProFileID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(areaID, "EQUIPMENT_POR_FILE") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("EquipmentProfileQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("EquipmentProfileList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtEquipmentProFileID.Text.Trim()))
            {
                this.lblMsg.Text = "请输入设备类型代码!";
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateEquipmentProfileInfo();
            }
            else
            {
                this.InsertEquipmentProfileInfo();
            }
        }

        private void InsertEquipmentProfileInfo()
        {
            try
            {
                EquipmentProfileInfo equipmentProfile = new EquipmentProfileInfo {
                    EquipmentProfileID = this.txtEquipmentProFileID.Text.Trim(),
                    Descr = this.txtDescr.Text.Trim(),
                    SystemCreated = this.chkSysCreated.Checked ? "Y" : "N",
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UserID
                };
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(equipmentProfile, "EQUIPMENT_POR_FILE") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = equipmentProfile;
                this.hdnFlag.Value = "1";
                this.hdnEquipmentProfileID.Value = equipmentProfile.EquipmentProfileID;
                this.txtEquipmentProFileID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnEquipmentProfileID.Value = base.Request.QueryString["ID"];
                this.SetEquipmentProfileInfo2Form();
                this.hdnFlag.Value = "1";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetEquipmentProfileInfo2Form()
        {
            try
            {
                string areaID = this.hdnEquipmentProfileID.Value;
                EquipmentProfileInfo equipmentProfile = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(areaID, "EQUIPMENT_POR_FILE") as EquipmentProfileInfo;
                base.DataEntityViewState = equipmentProfile;
                if (equipmentProfile != null)
                {
                    this.txtEquipmentProFileID.Text = equipmentProfile.EquipmentProfileID;
                    this.txtDescr.Text = equipmentProfile.Descr;
                    this.txtMaximumWeight.Text = base.GetPreferenceDigit(equipmentProfile.MaximumWeight);
                    this.chkIsActive.Checked = equipmentProfile.IsActive == "Y";
                    this.chkSysCreated.Checked = equipmentProfile.SystemCreated == "Y";
                    this.txtEquipmentProFileID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateEquipmentProfileInfo()
        {
            try
            {
                EquipmentProfileInfo equipmentProfile = base.DataEntityViewState as EquipmentProfileInfo;
                equipmentProfile.Descr = this.txtDescr.Text.Trim();
                equipmentProfile.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                equipmentProfile.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(equipmentProfile, "EQUIPMENT_POR_FILE") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

